package vuelta.general.stage.finder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import vuelta.general.BaseActivity;
import vuelta.general.R;
import vuelta.general.adapter.StagesAdapter;
import vuelta.general.stage.StageDetailActivity;
import vuelta.general.util.Commons;
import vuelta.general.util.Parser;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;

public class StagesFinderActivity extends BaseActivity {
	private JSONArray mStagesArray = null;
	private StagesAdapter mStagesAdapter;
	private ListView mStagesList;
	private JSONArray mTappeArray;
	private EditText mSearchText;
	private RadioButton mOption1;
	private RadioButton mOption2;
	private RadioButton mOption3;
	private RadioButton mOption4;
	private Button mRicercaButton;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.stages_finder);
		initToolbar();

		mSearchText = (EditText) findViewById(R.id.search_text);
		mOption1 = (RadioButton) findViewById(R.id.option1);
		mOption2 = (RadioButton) findViewById(R.id.option2);
		mOption3 = (RadioButton) findViewById(R.id.option3);
		mOption4 = (RadioButton) findViewById(R.id.option4);
		mRicercaButton = (Button) findViewById(R.id.search);

		mSearchText.setOnKeyListener(new OnKeyListener() {
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {

					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(mSearchText.getWindowToken(), 0);

					return false;
				}
				return false;
			}
		});

		mRicercaButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				String checked = "";
				if (mOption1.isChecked()) {
					checked = "m";
				} else if (mOption2.isChecked()) {
					checked = "p";
				} else if (mOption3.isChecked()) {
					checked = "v";
				} else if (mOption4.isChecked()) {
					checked = "c";
				}

				if (mSearchText.getText().toString().length() != 0 || !checked.equals("")) {

					JSONArray jsonArray = Commons.getInstance().getStagesArray();
					JSONArray jsonTemp = new JSONArray();
					for (int i = 0; i < jsonArray.length(); i++) {
						try {
							JSONObject object = jsonArray.getJSONObject(i);

							if (mSearchText.getText().toString().length() != 0 && !checked.equals("")) {
								if (object.getString("nome").toLowerCase()
										.indexOf(mSearchText.getText().toString().toLowerCase()) != -1
										&& object.getString("tipo").equals(checked)) {
									jsonTemp.put(object);
								}
							} else if (mSearchText.getText().toString().length() != 0) {
								if (object.getString("nome").toLowerCase()
										.indexOf(mSearchText.getText().toString().toLowerCase()) != -1) {
									jsonTemp.put(object);
								}
							} else {
								if (object.getString("tipo").equals(checked)) {
									jsonTemp.put(object);
								}
							}

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					if (jsonTemp.length() == 0) {
						postToast("No result");

					} else {

						Commons.getInstance().setTappeArray(Parser.getInstance().createTappeArrayJson(jsonTemp));

						Intent intent = new Intent(StagesFinderActivity.this, StageDetailActivity.class);
						intent.putExtra("fromFinder", true);
						startActivity(intent);
					}
				}

			}
		});

	}
}
